function [A, B, Phi] = AIA_step1(I,Delta)
    [I_row, I_col] = size(I{1,1});
    A = nan(I_row, I_col);B = A;Phi = A;
    for r = 1:I_row
        for c = 1:I_col
            S = AIA_step1_S(Delta);
            R = AIA_step1_R(I,Delta, r, c);
            sum_Rnan = sum(isnan(R));
            if sum_Rnan == 0
                X = S \ R;
                A(r,c) = X(1);
                Phi(r,c) = atan2(-X(3), X(2));
                B(r,c) = sqrt(X(2)^2+X(3)^2);
            end
        end
    end
end

function R = AIA_step1_R(I,Delta,r,c)
    F = length(I);
    R11 = 0;R12 = 0;R13 = 0;
    for f = 1:F
        delta = Delta(f);
        i = I{f,1}(r,c);
        R11 = i + R11;
        R12 = i*cos(delta) + R12;
        R13 = i*sin(delta) + R13;
    end
    R = [R11, R12, R13];
    R = R';
end

function S = AIA_step1_S(Delta)
    F = length(Delta);
    S11 = F;
    S12 = cos(Delta);S12 = sum(S12(:));
    S13 = sin(Delta);S13 = sum(S13(:));
    S22 = cos(Delta).*cos(Delta);S22 = sum(S22(:));
    S23 = cos(Delta).*sin(Delta);S23 = sum(S23(:));
    S33 = sin(Delta).*sin(Delta);S33 = sum(S33(:));
    S = [S11, S12, S13;
         S12, S22, S23;
         S13, S23, S33];
end